import formatColumn from 'packages/BaseTableCell/formatColumn';
import { formatDate } from 'pub-bbx-utils';
import i18n from '@src/locales';

// 高级搜索
export const getAdvancedFields = (vm) => {
  let that = vm;
  return [
    {
      fieldName: 'state',
      displayName: i18n.t('common.base.isEnabled'),
      formType: 'select',
      setting: {
        isMulti: false, 
        dataSource: [
          {
            text: i18n.t('common.base.enable'),
            value: '1'
          },
          {
            text: i18n.t('common.base.disable'),
            value: '0'
          }
        ]
      }
    },
    {
      fieldName: 'type',
      displayName: i18n.t('common.form.preview.serviceItem.colum3'),
      formType: 'select',
      setting: {
        isMulti: false, 
        dataSource: that.serviceItemsTypes.map(item => {
          return {
            label: item,
            value: item
          }
        })
      }
    },
  ].map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: null,
      show: true,
      visable: typeof item.visable == 'undefined' ? true : item.visable,
      isSearch: 1,
      isNull: 1,
      isSystem: 1,
      orderId: 1
    }
  });
}

// 列表 && 选择列
export const getColumnFields = (vm) => {
  const that = vm;

  return [
    {
      fieldName: 'name',
      displayName: i18n.t('common.base.name'),
      renderCell: (h, col, row) => {
        return <div>
          <el-button type="text" onClick={e => that.handleOpenDetailView(row)}>{ row.name }</el-button>
        </div>
      }
    },
    {
      fieldName: 'serialNumber',
      displayName: i18n.t('common.base.serialNumber'),
      sortable: 'custom',
      renderCell: (h, col, row) => {
        return <div>{ row.serialNumber }</div>
      }
    },
    {
      fieldName: 'type',
      displayName: i18n.t('common.form.preview.serviceItem.colum3'),
      renderCell: (h, col, row) => {
        return <div>{ row.type }</div>
      }
    },
    {
      fieldName: 'unit',
      displayName: i18n.t('common.form.preview.serviceItem.colum4'),
      renderCell: (h, col, row) => {
        return <div>{ row.unit }</div>
      }
    },
    {
      fieldName: 'salePrice',
      displayName: i18n.t('servicePrice.list.displayName.salePrice'),
      sortable: 'custom',
      renderCell: (h, col, row) => {
        return <div>{ row.salePrice.toFixed(2) }</div>
      }
    },
    {
      fieldName: 'costPrice',
      displayName: i18n.t('servicePrice.list.displayName.deliveryPrice'),
      sortable: 'custom',
      renderCell: (h, col, row) => {
        return <div>{ row.costPrice.toFixed(2) }</div>
      }
    },
    {
      fieldName: 'productTypeList',
      displayName: i18n.t('common.label.ralationProductType'),
      sortable: 'custom',
      renderCell: (h, col, row) => {
        return <div>{ getProductTypeName(row.productTypeList) }</div>
      }
    },
    {
      fieldName: 'create_time',
      displayName: i18n.t('common.base.createTime'),
      sortable: 'custom',
      sortName: 'create_time',
      isSystem: 1,
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.createTime, 'YYYY-MM-DD HH:mm')}</div>;
      }
    },
    {
      fieldName: 'state',
      displayName: `${i18n.t('common.base.enable')}/${i18n.t('common.base.disable')}`,
      sortable: 'custom',
      renderCell: (h, col, row) => {
        return <div>
          <el-switch
                value={row.state}
                key={row.id}
                disabled={row.disabled}
                active-value={1} inactive-value={0}
                onChange={e => that.handleChangeStatus(row)}></el-switch>
              <span> {row.state == 1 ? i18n.t('common.base.enable') : i18n.t('common.base.disable')} </span>
        </div>
      }
    },
    {
      fieldName: 'operate',
      displayName: i18n.t('common.base.operation'),
      fixed: 'right',
      width: '88px',
      renderCell: (h, col, row) => {
        return <div>
          <el-button type="text" class={`${row.state == 0 ? 'disabled' : ''}`} disabled={row.state == 0} onClick={e => that.handleOpenEditView(row)}>{i18n.t('common.base.edit')}</el-button>
        </div>
      }
    },
  ].map(item => {
    // 初始化属性
    let field = {
      ...item,
      formType: 'text',
      setting: item.setting || {},
      isSystem: 1,
      isNull: 1,
      isSearch: 1,
      orderId: 1,
      show: true,
      tableName: 'event',
      visable: typeof item.visable == 'undefined' ? true : item.visable
    };
    return formatColumn(field); 
  })
};

export const getExportSystemInfo = () => {
  return [
    {
      exportAlias: "state",
      displayName: "事件状态",
    },
    {
      exportAlias: "createUser",
      displayName: "创建人",
    },
    {
      exportAlias: "executor",
      displayName: "负责人",
    },
    {
      exportAlias: "synergies",
      displayName: "协同人",
    },
    {
      exportAlias: "createTime",
      displayName: "创建时间",
    },
    {
      exportAlias: "allotTime",
      displayName: "分配时间",
    },
    {
      exportAlias: "startTime",
      displayName: "开始时间",
    },
    {
      exportAlias: "completeTime",
      displayName: "完成时间",
    },
    {
      exportAlias: "acceptUsedTime",
      displayName: "响应用时",
    },
    {
      exportAlias: "workUsedTime",
      displayName: "工作用时",
    },
    {
      exportAlias: "finishUsedTime",
      displayName: "事件用时",
    },
    {
      exportAlias: "evaluateTime",
      displayName: "评价时间",
    },
    {
      exportAlias: "evaluateState",
      displayName: "评价状态",
    },
    {
      exportAlias: "onceRollback",
      displayName: "曾回退",
    },
    {
      exportAlias: "degree",
      displayName: "满意度",
    },
    {
      exportAlias: "suggestion",
      displayName: "客户评价",
    },
    {
      exportAlias: "isTransferToTask",
      displayName: "是否创建工单",
    },
    {
      exportAlias: "inApprove",
      displayName: "审批状态",
    },
    {
      exportAlias: "taskNo",
      displayName: "关联工单",
    },
  ]
}

export const getProductTypeName = (arr) => {
  if (!arr || arr.length === 0) return '';
  return arr.map(v => v.catalogName).join('，');
}